package mysql

import (
	"gorm.io/gorm"
	"manuscript-service/servicerpc/servicerpc"
)

type Manuscript struct {
	gorm.Model
	Title   string `gorm:"type:varchar(30);not null;comment:稿件标题"`
	LikeNum int    `gorm:"type:int(11);not null;comment:点赞数量"`
	ClassId int    `gorm:"type:int(11);not null;comment:分类id"`
}

func (m *Manuscript) ManuscriptAdd(db *gorm.DB) error {
	return db.Debug().Create(m).Error
}

func (m *Manuscript) FindManuscriptByTitle(db *gorm.DB, title string) error {
	return db.Debug().Where("title = ?", title).First(m).Error
}

func ManuscriptList(db *gorm.DB) []*servicerpc.ManuscriptList {
	var list []*servicerpc.ManuscriptList
	db.Debug().Model(&Manuscript{}).
		Select(
			"manuscripts.id as manuscript_id",
			"manuscripts.title",
			"manuscripts.like_num",
			"classes.name as class_name",
			"manuscripts.created_at",
		).
		Joins("LEFT JOIN classes ON manuscripts.class_id = classes.id").
		Find(&list)
	return list
}
